/**
 * strictc - the Strict compiler
 * Copyright (C) 2008 David Olsson (vonolsson@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "ProgramNode.h"
#include "SourceFileNode.h"
#include "Visitor.h"

using namespace std;

ProgramNode::~ProgramNode() {
    for(list<SourceFileNode *>::iterator iter = m_sourceFiles.begin(); iter != m_sourceFiles.end(); iter++) {
        delete(*iter);
    }
}

void ProgramNode::addSourceFile(SourceFileNode *sourceFile) throw(bad_alloc) {
    if(!m_sourceFiles.empty()) {
        m_sourceFiles.back()->setNextSibling(sourceFile);
    }
    m_sourceFiles.push_back(sourceFile);
    sourceFile->setParent(this);
}

const Node *ProgramNode::firstChild() const throw() {
    if(!m_sourceFiles.empty()) {
        return(m_sourceFiles.front());
    }
    else {
        return(0);
    }
}

Node *ProgramNode::firstChild() throw() {
    if(!m_sourceFiles.empty()) {
        return(m_sourceFiles.front());
    }
    else {
        return(0);
    }
}

ClassNode *ProgramNode::classByName(const string &name) throw() {
    ClassNode *cls = 0;
    for(list<SourceFileNode *>::iterator iter = m_sourceFiles.begin(); iter != m_sourceFiles.end(); iter++) {
        if((cls = (*iter)->classByName(name)) != 0) {
            break;
        }
    }
    return(cls);
}

FunctionNode *ProgramNode::functionByName(const string &name) throw() {
    FunctionNode *func = 0;
    for(list<SourceFileNode *>::iterator iter = m_sourceFiles.begin(); iter != m_sourceFiles.end(); iter++) {
        if((func = (*iter)->functionByName(name)) != 0) {
            break;
        }
    }
    return(func);
}

InterfaceNode *ProgramNode::interfaceByName(const string &name) throw() {
    InterfaceNode *iface = 0;
    for(list<SourceFileNode *>::iterator iter = m_sourceFiles.begin(); iter != m_sourceFiles.end(); iter++) {
        if((iface = (*iter)->interfaceByName(name)) != 0) {
            break;
        }
    }
    return(iface);
}

NamespaceNode *ProgramNode::namespaceByName(const string &name) throw() {
    NamespaceNode *nc = 0;
    for(list<SourceFileNode *>::iterator iter = m_sourceFiles.begin(); iter != m_sourceFiles.end(); iter++) {
        if((nc = (*iter)->namespaceByName(name)) != 0) {
            break;
        }
    }
    return(nc);
}

void ProgramNode::preAccept(Visitor &visitor) throw() {
    visitor.preVisit(*this);
}

void ProgramNode::postAccept(Visitor &visitor) throw() {
    visitor.postVisit(*this);
}
